package org.jeecg.modules.front.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.ExtUserInfo;
import com.alipay.api.domain.InvoiceKeyInfo;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.domain.InvoiceInfo;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.SubMerchant;

import com.alipay.api.FileItem;
import org.jeecg.modules.gongqiang.entity.Good;
import org.jeecg.modules.gongqiang.entity.UserhomeOrder;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AliUtil {

    public String buyOrder(UserhomeOrder userhomeOrder, Good good) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        // 设置商户订单号
        model.setOutTradeNo(userhomeOrder.getOrderNum());
        // 设置订单总金额
        model.setTotalAmount(String.valueOf(userhomeOrder.getTprice()));
        // 设置订单标题
        model.setSubject(good.getName()+"*"+userhomeOrder.getNum());
        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        // 设置PC扫码支付的方式
        model.setQrPayMode("1");
        // 设置商户自定义二维码宽度
        model.setQrcodeWidth(100L);
        // 设置订单绝对超时时间
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time2=sdf.format(new Date(date.getTime()+1800000));
        model.setTimeExpire(time2);
        request.setBizModel(model);
        request.setNotifyUrl("https://gongqiangafter.gjlike.com/jeecg-boot/"+"front/order/ali/notify");
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        return pageRedirectionData;
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCbtWuyLzvmQscnfgu7oFbRlgVLA4C3KZidmYH8IITS7KLDviUknL9/OkGph8FTdH/QGIwK7L2vTpOdRidn7+b7J62qjWC4+H/nkkk5Pis/lb7zdEmNnyaTr//pXe9FvB5uk4QTplfSt4f6W0qfukzq9UoH+bMerCeGeqtuoXpR6AtNfQrMqOtz0KaiTcHq5PJurkDHvwkmXamtj2fFmZejmB7vc9sAM+2lb+bkBryYpMnDED9RhgkmUl00kz4GVvSJgC/DXn4H/sk/N+dPAz22T0wqTmCtByzc/+5/5VMnIZSHmwyJWnWaNtl4p3ks03F9LzlzCLo+UA3TGxy5v2nFAgMBAAECggEAU+BDdkWZZ60bKLvuSBDUE6xTh5gIcyIWEvdJHO7Lfe94siawTWuegEN6cX8ude0wmIFQluX2PAQA4lyTB5skerBiDZiAvMMn6y9TAGHr3CGnLe+7W22a0z2ghi5SoOb4IaqXIBqf/mNc/VDF/wrVOlANpAQvJ9umovtbc4nKoWmcszaviiZeKGMu6Af1Kx2W91YZyXd/cCB466k04Q82XHMEKemD1Z0dqR8IKdAl9jAyiRUCVpPVzsyxrfCc1TGocNZ3NYY87vOTcZ04uQg7oTOjzEOuTEszNnPiN2lLQnHTvn0ttsbT44R+ipVedG/mdadbvZTDo/i/TAQk9sFCAQKBgQDeFqKFbIOkijQK9Rfy3mnc+d2yuAWJ+MaAKwKcLjQuQ01/xOYNXCDRf1Zi/mBXxHRR8bTvFryGtaiVX0X2h9rn53P2mYf6SH3Agk8/+XgLDkxM84+V8gMEaaADHy2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abnzZwHcSoQxR8RS6gSQ+jH7WFiJ1SQ41IV+PsLo9mpzQhdESowCXR64Mo4l4jzzRftN6Pu4M9bC4wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021004158656388");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
